package org.dromara.iot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.iot.domain.IotUpkeepProject;
import org.dromara.iot.domain.bo.IotUpkeepProjectBo;
import org.dromara.iot.domain.vo.IotUpkeepProjectVo;
import org.dromara.iot.mapper.IotUpkeepProjectMapper;
import org.dromara.iot.service.IIotUpkeepProjectService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 电梯保养项目Service业务层处理
 *
 * @author lsl
 * @date 2024-06-14
 */
@RequiredArgsConstructor
@Service
public class IotUpkeepProjectServiceImpl implements IIotUpkeepProjectService {

    private final IotUpkeepProjectMapper baseMapper;

    /**
     * 查询电梯保养项目
     */
    @Override
    public IotUpkeepProjectVo queryById(Long projectId) {
        return baseMapper.selectVoById(projectId);
    }

    /**
     * 查询电梯保养项目列表
     */
    @Override
    public TableDataInfo<IotUpkeepProjectVo> queryPageList(IotUpkeepProjectBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<IotUpkeepProject> lqw = buildQueryWrapper(bo);
        Page<IotUpkeepProjectVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询电梯保养项目列表
     */
    @Override
    public List<IotUpkeepProjectVo> queryList(IotUpkeepProjectBo bo) {
        LambdaQueryWrapper<IotUpkeepProject> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<IotUpkeepProject> buildQueryWrapper(IotUpkeepProjectBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<IotUpkeepProject> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTemplateId() != null, IotUpkeepProject::getTemplateId, bo.getTemplateId());
        lqw.like(StringUtils.isNotBlank(bo.getProjectName()), IotUpkeepProject::getProjectName, bo.getProjectName());
        lqw.eq(StringUtils.isNotBlank(bo.getProjectRequire()), IotUpkeepProject::getProjectRequire, bo.getProjectRequire());
        lqw.eq(StringUtils.isNotBlank(bo.getProjectType()), IotUpkeepProject::getProjectType, bo.getProjectType());
        return lqw;
    }

    /**
     * 新增电梯保养项目
     */
    @Override
    public Boolean insertByBo(IotUpkeepProjectBo bo) {
        IotUpkeepProject add = MapstructUtils.convert(bo, IotUpkeepProject.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setProjectId(add.getProjectId());
        }
        return flag;
    }

    /**
     * 修改电梯保养项目
     */
    @Override
    public Boolean updateByBo(IotUpkeepProjectBo bo) {
        IotUpkeepProject update = MapstructUtils.convert(bo, IotUpkeepProject.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(IotUpkeepProject entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除电梯保养项目
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 根据模板ID查询项目列表
     *
     * @param templateId 模板ID
     * @return 项目列表
     */
    @Override
    public List<IotUpkeepProjectVo> queryListByTemplateId(Long templateId) {
        LambdaQueryWrapper<IotUpkeepProject> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(IotUpkeepProject::getTemplateId, templateId);
        return baseMapper.selectVoList(wrapper);
    }

    /**
     * 批量新增电梯保养项目
     *
     * @param upkeepProjectList 电梯保养项目列表
     * @return 是否新增成功
     */
    @Override
    public Boolean insertBatchByBo(List<IotUpkeepProjectBo> upkeepProjectList) {
        List<IotUpkeepProject> upkeepProjects = MapstructUtils.convert(upkeepProjectList, IotUpkeepProject.class);
        return baseMapper.insertBatch(upkeepProjects);
    }

    @Override
    public int deleteWithValidByTemplateIds(Collection<Long> templateIds) {
        LambdaQueryWrapper<IotUpkeepProject> wrapper = Wrappers.lambdaQuery();
        wrapper.in(IotUpkeepProject::getTemplateId, templateIds);
        return baseMapper.delete(wrapper);
    }
}
